package pdfsetter.core;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.apache.pdfbox.pdmodel.PDDocument;

import pdfsetter.core.pages.PageInfo;
import pdfsetter.core.pages.PageRectangle;
import pdfsetter.core.pages.PageType;
import pdfsetter.core.schema.CompoundDocumentSchemaPattern;
import pdfsetter.core.schema.DocumentSchema;
import pdfsetter.core.schema.DocumentSchemaPattern;
import pdfsetter.core.schema.SimpleDocumentSchemaPattern;
import pdfsetter.core.schema.SinglePageSchema;
import pdfsetter.core.schema.advanced.AttachedFilePageSelector;
import pdfsetter.core.schema.advanced.ConsecutiveFileListProcessPattern;
import pdfsetter.core.schema.advanced.FillingSchemaPattern;
import pdfsetter.core.schema.advanced.RepetitiveFileListProcessPattern;
import pdfsetter.core.schema.selectors.LeapingPageSelector;
import pdfsetter.impl.pdfbox.compiler.PdfBoxDocumentCompiler;

public class CompoundSettingExample {
    public static void main(String args[]) {
        System.out.println("Hello! This is compound setting example.");

        PageInfo a4page = new PageInfo(PageType.A4).rotate();
        ArrayList<PageRectangle> locations = new ArrayList<PageRectangle>();
        locations.add(new PageRectangle(0, 0, a4page.getWidth() / 2, a4page.getHeight(), 0));
        locations.add(new PageRectangle(a4page.getWidth() / 2, 0, a4page.getWidth() / 2, a4page.getHeight(), 0));
        SinglePageSchema pageSchema = new SinglePageSchema("2xA5 on A4", a4page, locations);

        ArrayList<PageRectangle> locations2 = new ArrayList<PageRectangle>();
        locations2.add(new PageRectangle(a4page.getWidth() / 4, 0, a4page.getWidth() / 2, a4page.getHeight(), 0));
        SinglePageSchema pageSchema2 = new SinglePageSchema("1xA5 on A4 in the middle", a4page, locations2);

        // ------------ Input Files --------------
        List<File> files = new LinkedList<File>();
        files.add(new File("src/test/resources/test-pdf-1.pdf"));
        files.add(new File("src/test/resources/test-pdf-2.pdf"));

        // Load source documents and retrieve document sizes
        int[] fileSizes = new int[files.size()];
        int i = 0;
        for (File file : files) {
            try {
                PDDocument pdd = PDDocument.loadNonSeq(file, null);
                fileSizes[i] = pdd.getNumberOfPages();
                System.out.println("File info: " + fileSizes[i] + "(pages)");
                pdd.close();
            } catch (IOException e) {
                e.printStackTrace();
                return;
            }
            i++;
        }
        // ------------ Use patterns and create FillingSchemas -------------
        ArrayList<AttachedFilePageSelector> asList = new ArrayList<AttachedFilePageSelector>();
        asList.add(new AttachedFilePageSelector(new LeapingPageSelector(2), 0)); // take every second from 0-file
        FillingSchemaPattern con1 = new ConsecutiveFileListProcessPattern(asList);
        asList.add(new AttachedFilePageSelector(new LeapingPageSelector(4), 1)); // take every forth from 1-file
        FillingSchemaPattern rep2 = new RepetitiveFileListProcessPattern(asList);

        // ------------ Create DocumentSchemaPatterns: Simple and Compound -------------
        DocumentSchemaPattern dspCon1 = new SimpleDocumentSchemaPattern(pageSchema2, con1); // pattern: 1-file / every second, only from 0-file / one per page
        DocumentSchemaPattern dspRep2 = new SimpleDocumentSchemaPattern(pageSchema, rep2); // pattern: 2-file // every second + every forth | repetitive / 2in1
        List<DocumentSchemaPattern> documentPatternsList = new LinkedList<DocumentSchemaPattern>();
        documentPatternsList.add(dspCon1);
        documentPatternsList.add(dspRep2);
        CompoundDocumentSchemaPattern cdsp = new CompoundDocumentSchemaPattern(documentPatternsList);

        // ------------ Create final DocumentSchema and compile it -------------
        DocumentSchema ds = cdsp.produceDocumentSchema(fileSizes);
        compile(ds, files, "out-test/out-compound_con1-rep2.pdf");
    }

    public static void compile(DocumentSchema doc, List<File> files, String outPath) {
        AdvancedSettingExample.docTest(doc);
        System.out.println("after test...");
        File out = new File(outPath);
        new PdfBoxDocumentCompiler().compile(doc, files, out);
        System.out.println("compilation finished!");
    }
}
